package ru.weho.osmwidget;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.util.DisplayMetrics;

public class OSMWidgetConfigure extends PreferenceActivity {
    static final String TAG = "OSMWidgetConfigure";

    public static final String PREFS_NAME = "ru.weho.OSMWidgetProvider";
    public static final String MAPTYPE_KEY = "osmwidget_maptype";
    public static final String ZOOM_KEY = "osmwidget_zoom";
    public static final String DENSITY = "osmwidget_density";

    public static final int defaultFrequency = 60000;
    public static final int defaultZoom = 12;
    
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    public OSMWidgetConfigure() {
        super();
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        
        SharedPreferences.Editor prefs = getBaseContext().getSharedPreferences(PREFS_NAME, 0).edit();
        prefs.putFloat(DENSITY, dm.density);
        prefs.commit();

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
        	mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        setResult(RESULT_CANCELED);

        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
        	finish();
        }

        addPreferencesFromResource(R.xml.widget_configure);

        Preference maptypePreference = (Preference)findPreference("maptype_preference");
        maptypePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
		        SharedPreferences.Editor prefs = getBaseContext().getSharedPreferences(PREFS_NAME, 0).edit();
		        prefs.putString(MAPTYPE_KEY + mAppWidgetId, (String)newValue);
		        prefs.commit();
		        return true;
			}
		});

        Preference zoomPreference = (Preference)findPreference("zoom_preference");
        zoomPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				int zoom = Integer.parseInt((String) newValue);
		        SharedPreferences.Editor prefs = getBaseContext().getSharedPreferences(PREFS_NAME, 0).edit();
		        prefs.putInt(ZOOM_KEY + mAppWidgetId, zoom);
		        prefs.commit();
		        return true;
			}
		});
    }

    @Override
    public void onBackPressed() {
    	final Context context = OSMWidgetConfigure.this;

		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		OSMWidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId);
		Intent resultValue = new Intent();
		resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
		setResult(RESULT_OK, resultValue);
		finish();
    }
}
